﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace ZKLibrarys.Constant
{
    /// <summary>
    /// 中控错误代码相关类
    /// </summary>
    public class ErrorCode
    {
        /// <summary>
        /// 连接成功
        /// </summary>
        [Description("连接成功")]
        public const int ConnectSuccess = 0;

        /// <summary>
        /// 调用接口失败
        /// </summary>
        [Description("调用接口失败")]
        public const int InterfaceFail = -1;

        /// <summary>
        /// 初始化失败
        /// </summary>
        [Description("调用接口失败")]
        public const int InitFail = -2;

        /// <summary>
        /// 初始化参数失败
        /// </summary>
        [Description("初始化参数失败")]
        public const int InitParaFail = -3;

        /// <summary>
        /// 读取数据模式错误
        /// </summary>
        [Description("读取数据模式错误")]
        public const int ReadDataModeFail = -5;

        /// <summary>
        /// 密码错误
        /// </summary>
        [Description("密码错误")]
        public const int PasswordError = -6;

        /// <summary>
        /// 回复错误
        /// </summary>
        [Description("回复错误")]
        public const int ReplyError = -7;

        /// <summary>
        /// 接收超时
        /// </summary>
        [Description("接收超时")]
        public const int ReceiveTimeout = -8;

        /// <summary>
        /// 连接超时
        /// </summary>
        [Description("连接超时")]
        public const int ConnTimeout = -307;

        /// <summary>
        /// 获取中文错误信息
        /// </summary>
        /// <param name="errCode">错误代码</param>
        /// <returns>中文错误信息</returns>
        public static string GetErrMsg(int errCode)
        {
            foreach (FieldInfo item in typeof(ErrorCode).GetFields())
            {
                if (Convert.ToInt32(item.GetValue(typeof(Int32))).Equals(errCode))
                {
                    return ((DescriptionAttribute)item.GetCustomAttribute(typeof(DescriptionAttribute))).Description;
                }
            }
            return "NULL";
        }
    }
}
